//
//  MainTabbarViewController.m
//  meiyinxiang
//
//  Created by apple on 2017/10/17.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MainTabbarViewController.h"
#import "MXYNavigationViewController.h"
#import "LuckyMoneyViewController.h"
#import "MYY_LoginViewController.h"
#import <SVProgressHUD.h>

#import <HexColor.h>
#import "AppDelegate.h"
#import <NIMKit.h>
@interface MainTabbarViewController ()
@end

@implementation MainTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBar.backgroundColor = [UIColor whiteColor];
    self.tabBar.translucent = NO;
    [self.tabBar setBarTintColor:[UIColor whiteColor]];
    [self setupChildControlles];
    [self setupSendLuckyMoneyButton];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)setupChildControlles{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"main" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = [[NSError alloc] init];
    NSArray *dictArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:5];
    for (NSDictionary *dic in dictArray) {
        UIViewController *subVC = [self viewControllers:dic];
        [vcArray addObject:subVC];
    }
    
    self.viewControllers = vcArray;
    NSLog(@"filedict is %@", dictArray);
}
- (UIViewController *)viewControllers:(NSDictionary *)dict{
    Class vc =  NSClassFromString(dict[@"clsName"]);
    UIViewController *subVc = [[vc alloc] init];
    subVc.title = dict[@"title"];
    NSString *imageName = dict[@"imageName"];
    NSString *normalImageString = [NSString stringWithFormat:@"%@",imageName];
    NSString *selectImageString = [NSString stringWithFormat:@"%@_selected",imageName];
    UIImage *normaolImage = [UIImage imageNamed:normalImageString];
    UIImage *selectImage = [UIImage imageNamed:selectImageString];
    [subVc.tabBarItem setImage:normaolImage];
    [subVc.tabBarItem setSelectedImage:selectImage];
    subVc.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    //设置字体标题颜色和大小
    [subVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"ea4f48"]} forState:UIControlStateHighlighted];
    [subVc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11]} forState:UIControlStateNormal];
    MXYNavigationViewController *navVC = [[MXYNavigationViewController alloc] initWithRootViewController:subVc];
    return navVC;
}
- (void)setupSendLuckyMoneyButton{
    UIButton *luckyMoneyButton = [[UIButton alloc] init];
    [luckyMoneyButton setImage:[UIImage imageNamed:@"media_sendmoney"] forState:UIControlStateNormal];
    //计算button的位置和大小
    NSInteger count = self.childViewControllers.count;
    CGFloat with = self.tabBar.bounds.size.width / count;
    CGFloat height = self.tabBar.bounds.size.height;
    luckyMoneyButton.frame = CGRectMake(with*2, 0, with, height);
    [luckyMoneyButton addTarget:self action:@selector(sendLuckyMoney) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBar addSubview:luckyMoneyButton];
}
#pragma mark - Actions
- (void)sendLuckyMoney{
    if ([[MYY_User readUserFromFile] token].length > 0) {
        LuckyMoneyViewController *luckyVC = [[[UINib nibWithNibName:@"LuckyMoneyViewController" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil] lastObject];
        MXYNavigationViewController *luckyNav = [[MXYNavigationViewController alloc] initWithRootViewController:luckyVC];
        luckyNav.navigationBar.tintColor = [UIColor whiteColor];
   //     [self.navigationController pushViewController:luckyNav animated:YES];
        [self presentViewController:luckyNav animated:YES completion:^{
            
        }];
//        [ApplicationDelegate.window.rootViewController presentViewController:luckyNav animated:YES completion:^{
//            NSLog(@"finish present");
//        }];
    }else{
        MYY_LoginViewController *login = [[MYY_LoginViewController alloc] init];
        MXYNavigationViewController *nvc = [[MXYNavigationViewController alloc] initWithRootViewController:login];
        [self presentViewController:nvc animated:YES completion:nil];
    }
   
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MYX_LoginOutNotificationName object:nil];
}
@end
